package com.bjqh.edu.UI.Activity.StudentPersonal;

import android.app.ProgressDialog;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.SearchView;
import android.widget.TextView;
import android.widget.Toast;

import com.bjqh.edu.Base.AppConstants;
import com.bjqh.edu.Bean.HistoricalGrade;
import com.bjqh.edu.R;
import com.bjqh.edu.UI.Activity.Login.LoginActivity;
import com.bjqh.edu.UI.dialog.LoadDialog;
import com.bjqh.edu.adapter.HistoricalGradeAdapter;
import com.google.gson.Gson;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.cache.CacheMode;
import com.lzy.okgo.callback.StringCallback;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import okhttp3.Call;
import okhttp3.Response;

public class HistoricalGradeActivity extends AppCompatActivity {

    @BindView(R.id.iv_back)
    ImageView ivBack;
    @BindView(R.id.tv_title)
    TextView tvTitle;
    @BindView(R.id.tv_qx)
    TextView tvQx;
    @BindView(R.id.rl_top_tilte)
    RelativeLayout rlTopTilte;
    @BindView(R.id.listView)
    ListView listView;
    private HistoricalGradeAdapter adapter;
    List<HistoricalGrade.RowsBean> rowsBeen;
    private LoadDialog dialog;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //全屏
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
                WindowManager.LayoutParams.FLAG_FULLSCREEN);
        setContentView(R.layout.activity_historical_grade);
        ButterKnife.bind(this);
        ivBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });
        tvTitle.setText("历史成绩");
        dialog = new LoadDialog(this,"火速加载中...");
        dialog.initDialog(this,"火速加载中...");
        OkGo.post(AppConstants.BASE_URL + "/grade/datalist.shtml")
                .tag(this)
                .cacheKey("cacheKey")
                .cacheMode(CacheMode.DEFAULT)
                .execute(new StringCallback() {
                    @Override
                    public void onSuccess(String s, Call call, Response response) {
                      LoadDialog.closeDialog();
                        Log.e("历史成绩",s);
                        Gson gson = new Gson();

                        HistoricalGrade historicalGrade = gson.fromJson(s, HistoricalGrade.class);
                        if(historicalGrade.getMsg()!=null)
                        {
                            if(historicalGrade.getMsg().equals("timeout"))
                            {
                                AppConstants.gotoLogin(HistoricalGradeActivity.this);

                            }
                            else if(historicalGrade.getMsg().equals("remote login"))
                            {
                                AppConstants.remologin(HistoricalGradeActivity.this);
                            }

                        }
                        else
                        {
                            rowsBeen=historicalGrade.getRows();
                            adapter=new HistoricalGradeAdapter(HistoricalGradeActivity.this,rowsBeen);
                            listView.setAdapter(adapter);

                        }


                    }

                    @Override
                    public void onError(Call call, Response response, Exception e) {
                        super.onError(call, response, e);
                        Toast.makeText(HistoricalGradeActivity.this, "报告小主，服务器被外星人偷走啦~~", Toast.LENGTH_SHORT).show();
                        LoadDialog.closeDialog();
                    }
                });

    }

}
